package com.example.mycount2text;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity_height extends AppCompatActivity implements View.OnClickListener {
    ArrayList<String> table = new ArrayList<>();
    private int UpSave;
    private int DownSave;
    int ThePointTrigger=0;
    private Integer TheMsg;
    private Spinner spinner, spinner2;
    private boolean UpTrigger = true, DownTrigger = false, Binary_Trigger = false, Octal_Trigger = false, Ten_Trigger = false, Six_Trigger = false,
            TheUpFault = true, TheDownFault = true;
    private Button one, two, three, four, five, six, seven, eight, nine, zero, del, point, ac,cul,weight,Length,cp;
    private TextView Up, Down;

    @Override
    public void onClick(View view) {
        Intent Cintent =new Intent(this,MainActivity.class);
        Intent Lintent =new Intent(this,MainActivity_weight.class);
        Intent Tintent =new Intent(this,MainActivityExchange.class);
        switch (view.getId()) {
            case R.id.Up:
                OnTrigger("Up");
                break;
            case R.id.Down:
                OnTrigger("Down");
                getInfoDown();
                break;
            case R.id.one:
                if (FindTrigger() != null) {
                    System.out.println("Button is" + FindTrigger().toString());
                    if (InfoError(1)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "1");
                        getInfoDown();
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "Button is" + FindTrigger().toString(), Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.two:
                if (FindTrigger() != null) {
                    if (InfoError(2)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "2");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字1!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.three:
                if (FindTrigger() != null) {
                    if (InfoError(3)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "3");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字2!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error3", Toast.LENGTH_SHORT).show();
                break;
            case R.id.four:
                if (FindTrigger() != null) {
                    if (InfoError(4)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "4");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字3!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.five:
                if (FindTrigger() != null) {
                    if (InfoError(5)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "5");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字4!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.six:
                if (FindTrigger() != null) {
                    if (InfoError(6)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "6");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字5!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.seven:
                if (FindTrigger() != null) {
                    if (InfoError(7)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "7");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字6!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.eight:
                if (FindTrigger() != null) {
                    if (InfoError(8)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "8");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字7!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nine:
                if (FindTrigger() != null) {
                    if (InfoError(9)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "9");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字8!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.zero:
                if (FindTrigger() != null) {
                    if (InfoError(0)) {
                        FindTrigger().setText(FindTrigger().getText().toString() + "0");
                        Transform(FindTrigger().getText().toString());
                    } else
                        Toast.makeText(this, "请输入规格范围内的数字9!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(this, "Input Error1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ac:
                Up.setText("");
                Down.setText("");
                ThePointTrigger=0;
                break;
            case R.id.del1:
                if (FindTrigger().getText().toString().length() != 0) {
                    FindTrigger().setText(FindTrigger().getText().toString().substring(0, FindTrigger().getText().toString().length() - 1));
                    Transform(FindTrigger().getText().toString());
                } else
                    Toast.makeText(this, "Input Error", Toast.LENGTH_SHORT).show();
                break;
            case R.id.point:
                if (!FindTrigger().getText().toString().equals("")&&ThePointTrigger!=1) {
                    FindTrigger().setText(FindTrigger().getText().toString() + ".");
                    ThePointTrigger=1;
                    Transform(FindTrigger().getText().toString());
                } else {
                    System.out.println("触发了！");
                    Toast.makeText(this, "Input Error", Toast.LENGTH_SHORT).show();}
                break;
            case R.id.compute:
                startActivity(Cintent);
                break;
            case R.id.Length:
                startActivity(Lintent);
                break;
            case R.id.cul:
                startActivity(Tintent);
                break;
        }

    }

    private void AllInit() {

        cp=findViewById(R.id.compute);
        cp.setOnClickListener(this);
        cul=findViewById(R.id.cul);
        cul.setOnClickListener(this);
        weight=findViewById(R.id.weight);
        weight.setOnClickListener(this);
        Length=findViewById(R.id.Length);
        Length.setOnClickListener(this);
        ac = findViewById(R.id.ac);
        spinner = findViewById(R.id.spinner);
        spinner2 = findViewById(R.id.spinner2);
        Up = findViewById(R.id.Up);
        Down = findViewById(R.id.Down);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        four = findViewById(R.id.four);
        five = findViewById(R.id.five);
        six = findViewById(R.id.six);
        seven = findViewById(R.id.seven);
        eight = findViewById(R.id.eight);
        nine = findViewById(R.id.nine);
        zero = findViewById(R.id.zero);
        del = findViewById(R.id.del1);
        point = findViewById(R.id.point);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        Up.setOnClickListener(this);
        Down.setOnClickListener(this);
        del.setOnClickListener(this);
        ac.setOnClickListener(this);
        point.setOnClickListener(this);
        table.add("千米 km");
        table.add("米 m");
        table.add("分米 dm");
        table.add("厘米 cm");
        table.add("毫米 mm");
        table.add("微米 μm");
        table.add("纳米 nm");
        ArrayAdapter<String> tableAdapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_dropdown_item,
                table
        );
        spinner.setAdapter(tableAdapter);
        spinner2.setAdapter(tableAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (TheUpFault == false) {
                    InfoInit();
                    Log.v("Table是", table.get(i));
                    switch (table.get(i)) {
                        case "千米 km":
                            UpSave = 0;
                            break;
                        case "米 m":
                            UpSave = 1;
                            break;
                        case "分米 dm":
                            UpSave = 2;
                            break;
                        case "厘米 cm":
                            UpSave = 3;
                            break;
                        case "毫米 mm":
                            UpSave = 4;
                            break;
                        case "微米 μm":
                            UpSave = 5;
                            break;
                        case "纳米 nm":
                            UpSave = 6;
                            break;
                    }
                    getInfoUp();
                } else if (TheUpFault) {
                    Log.v("Fault:", "The down is " + TheDownFault);
                    spinner.setSelection(1, true);
                    TheUpFault = false;
                    UpSave = 1;
                    Ten_Trigger = true;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (!TheDownFault) {
                    InfoInit();
                    switch (table.get(i)) {
                        case "千米 km":
                            DownSave = 0;
                            break;
                        case "米 m":
                            DownSave = 1;
                            break;
                        case "分米 dm":
                            DownSave = 2;
                            break;
                        case "厘米 cm":
                            DownSave = 3;
                            break;
                        case "毫米 mm":
                            DownSave = 4;
                            break;
                        case "微米 μm":
                            DownSave = 5;
                            break;
                        case "纳米 nm":
                            DownSave = 6;
                            break;
                    }
                    getInfoDown();
                } else if (TheDownFault) {
                    Log.v("Fault:", "The down is " + TheDownFault);
                    spinner2.setSelection(3, true);
                    DownSave = 3;
                    TheDownFault = false;
                }
            }


            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_weight);
        AllInit();

    }

    private int TheTriggerSave() {
        if (UpTrigger) {
            return UpSave;
        } else
            return DownSave;
    }

    private int ReTheTriggerSave() {
        if (UpTrigger) {
            return DownSave;
        } else
            return UpSave;
    }

    private String FindTriggerInfo() {
        if (UpTrigger) {
            return "Up";
        } else
            return "Down";
    }

    private TextView FindTrigger() {
        if (UpTrigger) {
            return Up;
        } else
            return Down;
    }

    private TextView ReFindTrigger() {
        if (UpTrigger) {
            return Down;
        } else
            return Up;
    }

    private void OnTrigger(String TheTrigger) {
        if (TheTrigger.equals("Up")) {
            UpTrigger = true;
            DownTrigger = false;
        } else {
            UpTrigger = false;
            DownTrigger = true;
        }
    }

    private void Transform(String TheX) {
        int TheSender = TheTriggerSave();
        int TheReceiver = ReTheTriggerSave();
        Double YouNeed = Double.valueOf(TheX) ;
        Log.v("TheSender", "is" + TheSender);
        Log.v("TheReceiver", "is" + TheReceiver);
        Log.v("TheMsg", "is" + TheX);
        if (TheSender==TheReceiver){
            ReFindTrigger().setText(YouNeed.toString());
            return;
        }
        if(TheSender==5&&TheReceiver==6)
        {
            YouNeed=YouNeed*1000;
            ReFindTrigger().setText(YouNeed.toString());
            return;
        }
        if(TheSender==6&&TheReceiver==5)
        {
            YouNeed=YouNeed/1000;
            ReFindTrigger().setText(YouNeed.toString());
            return;
        }

        switch (TheSender) {
            case 0:
                YouNeed = YouNeed*1000;
                break;
            case 1:

                break;
            case 2:
                YouNeed = YouNeed/10;
                break;
            case 3:
                YouNeed = YouNeed/100;
                break;
            case 4:
                YouNeed = YouNeed/1000;
                break;
            case 5:
                YouNeed = YouNeed/1000000;
                break;
            case 6:
                YouNeed = YouNeed / 1000000000;
                break;
        }
        Log.v("TheMsg", "is");
        switch (TheReceiver) {
            case 0:
                YouNeed=YouNeed/1000;
                ReFindTrigger().setText(YouNeed.toString());
                break;
            case 1:
                ReFindTrigger().setText(YouNeed.toString());
                break;
            case 2:
                YouNeed =YouNeed*10;
                ReFindTrigger().setText(YouNeed.toString());
                break;
            case 3:
                YouNeed =YouNeed*100;
                ReFindTrigger().setText(YouNeed.toString());
                break;
            case 4:
                YouNeed =YouNeed*1000;
                ReFindTrigger().setText(YouNeed.toString());
                break;
            case 5:
                YouNeed =YouNeed*1000000;
                ReFindTrigger().setText(YouNeed.toString());
                break;
            case 6:
                YouNeed =YouNeed*1000000000;
                ReFindTrigger().setText(YouNeed.toString());
                break;
        }
    }



    private Boolean InfoError(int i){
        return true;
    }

    private void InfoInit(){
        Up.setText("");
        Down.setText("");
        ThePointTrigger=0;
    }

    private void getInfoUp(){
        Log.v("\n现在Up的选取是:",""+UpSave);
    }

    private void getInfoDown(){
        Log.v("\n现在Down的选取是:",""+DownSave);
    }

}
